﻿using System;
using FireStarter.Base;
using FireStarter.Base.BusinessObjectMerger;
using FireStarterModeller.BO;
using FireStarterModeller.Logic.Wizards;
using Habanero.Base;
using Habanero.BO;
using Microsoft.Practices.Unity;

namespace FireStarterModeller.Logic
{
    public class ClassDefToExistingSolutionImporter : ClassDefToSolutionImporter
    {
     

        public ClassDefToExistingSolutionImporter(IUnityContainer iocContainer, string filename, DMSolution existingSolution) : base(iocContainer, filename)
        {
            Solution = existingSolution;
        }

        public override DMSolution Import()
        {
            OnImportProgressChanged(30);
            FixUITabNames(Solution);
            Solution.Save();
            OnImportProgressChanged(35);
            var tempSolutionName = Solution.SolutionName;
            var tempDefaultAssemblyName = Solution.DefaultAssembly.AssemblyName;
            ((IMergableBo)TempSolution).PendingChangeType = ChangeType.None;
            OnImportProgressChanged(40);
            try
            {
                MarkAllSolutionAssembliesForDelete(TempSolution);
                OnImportProgressChanged(45);
                LoadClassDefsToSolutionForImport(TempSolution);
                OnImportProgressChanged(60);
                UpdateSolutionNames(TempSolution);
                OnImportProgressChanged(65);
                FixUITabNames(TempSolution);
                OnImportProgressChanged(70);
                FMDataStore.ResetDataAccessor();
                OnImportProgressChanged(75);
                MergeSolutions(TempSolution, Solution);
                OnImportProgressChanged(85);
                var solutionTraverser = new SolutionTraverser();
                solutionTraverser.MarkForDeleteAllChangeTypeDelete(Solution);
                OnImportProgressChanged(90);
                ProcessLoadedSolution(Solution, IocContainer);
                OnImportProgressChanged(95);
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }

            Solution.SolutionName = tempSolutionName;
            Solution.DefaultAssembly.AssemblyName = tempDefaultAssemblyName;
            Solution.Save();

            //Ensure that the Temp solution is removed from the _dataStoreInMemory
            DeleteTempSolution();
            OnImportProgressChanged(99);

            return Solution;
                
        }


        protected static void MergeSolutions(DMSolution fromSolution, DMSolution toSolution)
        {
            IMergeBO<DMSolution> merger = new MergeBO<DMSolution>();
            toSolution = merger.MergeBOFrom(fromSolution, toSolution);

            merger.UpdatePendingChangeType(toSolution);
        }

        protected static void FixUITabNames(IDMSolution dmSolution)
        {
            if (dmSolution == null) throw new ArgumentNullException("dmSolution");
            dmSolution.AllClasses.ForEach(
                @class => @class.UIViews.ForEach(
                    view =>
                    {
                        UIFormInfo info = view.UIFormInfo;
                        if (info == null) return;
                        info.UITabs.ForEach(
                            tab => { if (String.IsNullOrEmpty(tab.Name)) tab.Name = "default"; });
                    }));
        }

        private void DeleteTempSolution()
        {
            new DMSolutionDeleter(TempSolution).Delete();
        }

    }
}
